package com.sgb.kjwl.view.ui.fragment.pick;

import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.NewFragmentPickListBinding;
import com.sgb.kjwl.di.pick.DaggerPickFragComponent;
import com.sgb.kjwl.di.pick.PickFragModule;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.model.entity.ProvinceEntity;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.model.entity.pick.TenderAllListNewEntity;
import com.sgb.kjwl.view.event.ChooseAreaEvent;
import com.sgb.kjwl.view.event.special.QuickEntryEvent;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.pick.PickNoticeDetailActivity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.sgb.kjwl.view.ui.adapter.pick.PickBidingAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickHallAllNewAdapter;
import com.sgb.kjwl.view.ui.adapter.pick.PickNoticeListAdapter;
import com.sgb.kjwl.view.ui.quickentry.BackResultContentListener;
import com.sgb.kjwl.view.ui.quickentry.QuickEntryEntity;
import com.sgb.kjwl.view.widget.PickHallBidingPopWindow;
import com.sgb.kjwl.view.widget.pick.PickChooseLeftDialog2;
import com.sgb.kjwl.view.widget.pick.PickPopWindowView;
import com.sgb.kjwl.viewmodel.pick.PickListsFragmentModel;
import com.sgb.link.utils.PickCateType;
import com.sgb.link.utils.SizeUtils;
import com.sgb.zhaocai.entity.DemandForecastListBean;
import com.sgb.zhaocai.entity.DemandForecastListEntity;
import com.sgb.zhaocai.utils.PickCopyUtils;
import com.sgb.zhaocai.view.demand.adapter.DemandListAdapter;
import com.sgb.zhaocai.utils.PickUtil;
import com.sgb.zhaocai.widget.threefliter.ZCThreeLevelFilterWindow;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.event.EventConstant;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.PickSelectTextEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.areapopup.GoodsSourceLocationEntity;
import com.swgk.core.view.areapopup.ThreeLevelAreaWindow;
import com.swgk.core.view.selection.CommonPopEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 招标-列表
 */
public class PickListsFragment extends AppLazyFragment implements PickChooseLeftDialog2.DialogCallBackImpl {

    @Inject
    PickListsFragmentModel viewModel;
    NewFragmentPickListBinding binding;

    private PickHallAllNewAdapter pickBidingAdapter;
    private PickNoticeListAdapter pickNoticeListAdapter;
    private PickHallAllNewAdapter pickHallAdapter;
    private PickHallAllNewAdapter pickHallAllNewAdapter;
    private DemandListAdapter demandlistadapter;

    private ThreeLevelAreaWindow mAddressPop;
    private PickHallBidingPopWindow pickMoneyPopWindow;
    private PickHallBidingPopWindow pickSortPopWindow;
    private PickHallBidingPopWindow pickTabLayoutPopWindow;
    private PickHallBidingPopWindow pickBidTypePopWindow;
    private PickHallBidingPopWindow applyPopWindow;
    private PickHallBidingPopWindow statusPopWindow;
    private ZCThreeLevelFilterWindow popCategory;
    private PickHallBidingPopWindow pushTimeWindow;
    private PickHallBidingPopWindow pickSourcesPopWindow;
    private PickHallBidingPopWindow pickBidMethodPopWindow;

    private List<CommonPopEntity> commonlist;
    private List<CommonPopEntity> childrenCommonlist;
    private List<PickSelectEntity> typeDatas = new ArrayList<>();
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData;
    private List<PickSelectEntity> sortData = new ArrayList<>();
    private List<PickSelectEntity> tabTypeList = new ArrayList<>();
    private List<PickSelectEntity> startTimeData = new ArrayList<>();
    private List<PickSelectEntity> endTimeData = new ArrayList<>();
    private List<PickSelectEntity> pushTimeData = new ArrayList<>();
    private List<PickSelectEntity> statusData = new ArrayList<>();
    private List<PickSelectEntity> priceData = new ArrayList<>();
    private List<PickSelectEntity> signData = new ArrayList<>();
    private List<PickSelectEntity> bidTypeData = new ArrayList<>();
    private List<PickSelectEntity> dataSources = new ArrayList<>();
    private List<PickSelectEntity> bidMethodData = new ArrayList<>();

    //1-招标大厅  2-招标公告
    public static final String FROM_WHERE = "FROM_WHERE";
    private String fromWhere;
    private int tabPosition = 0;
    private int tabTypePosition;

    //省
    private String areacode;
    private String areaName;
    //地区
    private String cityNo;
    //招标形式
    private String bidTypeName = "招标形式";
    private String bidTypeNo = "0";
    //状态(1、即将招标,2、正在招标,3、竞标评标,4、招标定标,5、废标,6、再次招标,7、流标,8、招标预告,9、招标报名,10、资格审查中)
    private String statusNo = "";
    private String statusName;
    //开始时间
    private String startTime;
    //截止时间
    private String endTime;
    //发布时间
    private String pushTimeNo = "0";
    private String pushTimeName;
    //报名方式
    private String tenderApplyNo = "";
    private String tenderApplyName;
    //规模
    private String price;
    private String priceName = "";
    //类型
    private String typeNo = "0";
    //排序类型
    private String sortType = "1";
    private String sortTypeName;
    //数据来源
    private String sourcesType = "0";
    private String sourcesName;
    //数据来源
    private String bidMethodType = "0";
    private String bidMethodName;
    //类别二级
    private String goodsType;
    private String goodsTypeName;

    //搜索关键字
    private String searchName = "";
    private boolean isSearching = false;

    private DemandForecastListBean requestBean;
    private String longitude = "";
    private String latitude = "";
    private boolean isLoadMore = false;
    private boolean isFirst = false;
    private boolean isLoadData = false;
    private int page = 1;
    private boolean isEnableLoadMore = false; //是否可以上拉加载

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickFragComponent.builder()
                .appComponent(appComponent)
                .pickFragModule(new PickFragModule(this))
                .build()
                .inject(this);
    }

    public static PickListsFragment newInstance(String fromWhere, int tabPosition, int tabTypePosition,String searchName) {
        PickListsFragment fragment = new PickListsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FROM_WHERE, fromWhere);
        bundle.putInt("tabPosition", tabPosition);
        bundle.putInt("tabTypePosition", tabTypePosition);
        bundle.putString("searchName",searchName);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.new_fragment_pick_list, container, false);
        if (getArguments() != null) {
            fromWhere = (String) getArguments().get(FROM_WHERE);
            searchName = getArguments().getString("searchName");
        }
        return binding.getRoot();
    }


    private void init() {
        initChooseDialog();

        // 金额
        pickMoneyPopWindow = new PickHallBidingPopWindow(getContext());
        pickMoneyPopWindow.setBgView(binding.grayLayout);
        pickMoneyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickCateType.TYPE_MONEY, (Integer) key3);
                initFilterData(bidTypeNo);
            }
        });
        pickMoneyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickCateType.TYPE_MONEY);
                updateChooseTextStatus(binding.tvMoney, value > 0 ? 1 : 0, priceName);
            }
        });

        // 报名方式   现资格审查方式
        applyPopWindow = new PickHallBidingPopWindow(getContext());
        applyPopWindow.setBgView(binding.grayLayout);
        applyPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                updateChooseStatus(PickCateType.TYPE_SIGN, (Integer) key3);
                initFilterData(bidTypeNo);
            }
        });
        applyPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickCateType.TYPE_SIGN);
                updateChooseTextStatus(binding.tvApply, value > 0 ? 1 : 0, tenderApplyName);
            }
        });

        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                tabPosition = tab.getPosition();
                typeNo = typeDatas.get(tab.getPosition()).getNo();
                if (tabPosition == 0) {
                    typeNo = "0";
                }
                viewModel.tenderWayByType(typeNo, fromWhere);
                if (tabPosition == 0 || tabPosition == (typeDatas.size() - 1)) {
                    binding.tvCategory.setVisibility(View.GONE);
                    if (mData != null && mData.size() > 0 && mData.get(0) != null) {
                        if (mData.get(0).getTypeName().contains("类别")) {
                            mData.remove(0);
                        }
                    }
                    if (tabPosition == (typeDatas.size() - 1)) {
                        binding.tvCategory.setVisibility(View.GONE);
                    }
                    binding.lineTabType.setVisibility(View.GONE);
                    initFilterData(bidTypeNo);
                } else {
                    getCategoryAllList();
                    binding.lineTabType.setVisibility(View.VISIBLE);
                    binding.tvCategory.setVisibility(View.GONE);
                }

                if (isFirst) {
                    bidTypeName = "招标形式";
                    bidTypeNo = "0";
                    updateChooseTextStatus(binding.tvBidType, 0, bidTypeName);
                    pickBidTypePopWindow.setDefItem();
                    initAdapter();
                    clearViewShow();
                }
                isFirst = true;

                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_main));
                    tabSelect.setTextSize(14);
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_333333));
                    tabSelect.setTextSize(14);
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }

        });

        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabTypePosition = tab.getPosition();
                if (tabTypePosition == 0) {
                    binding.tvCategory.setVisibility(View.GONE);
                } else {
                    childrenCommonlist = commonlist.get(tabTypePosition).getChildren();
                    if (childrenCommonlist == null || childrenCommonlist.size() == 0) {
                        binding.tvCategory.setVisibility(View.GONE);
                    } else {
                        goodsType = "";
                        goodsTypeName = "";
                        updateChooseTextStatus(binding.tvCategory, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
                        binding.tvCategory.setVisibility(View.VISIBLE);
                        popCategory.setShowThreeRecycler(false);
                        popCategory.setDismiss(false);
                        popCategory.setData(childrenCommonlist, 0);
                        if (!isLoadData) {
                            refreshDataInfo(PickCateType.TYPE_CATEGORY_1, "", commonPopEntityToPickSelectEntity(childrenCommonlist));
                        }
                    }
                }
                pickTabLayoutPopWindow.setDefItem(tabTypePosition);
                goodsType = tabTypeList.get(tabTypePosition).getNo();
                DemandData();
                initFilterData(bidTypeNo);

                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTextSize(14);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tabSelect.setTextColor(getResources().getColor(R.color.APPTabColor));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
                    tabSelect.setTextSize(14);
                    tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tabSelect.setTextColor(getResources().getColor(R.color.color_666666));
                    tabSelect.invalidate();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //类别二级
        pickTabLayoutPopWindow = new PickHallBidingPopWindow(getContext());
        pickTabLayoutPopWindow.setBgView(binding.grayLayout);
        pickTabLayoutPopWindow.setOnResultClick((key1, key2, key3) -> {
            binding.tabLayoutType.getTabAt((int) key3).select();
            pickTabLayoutPopWindow.setDefItem((int) key3);
            goodsType = ((PickSelectEntity) key1).getNo();
            initFilterData(bidTypeNo);
        });
        pickTabLayoutPopWindow.setOnDisMissClick(() -> {
            binding.surveyView.setImageResource(R.drawable.btn_down);
        });

        //排序方式
        pickSortPopWindow = new PickHallBidingPopWindow(getContext());
        pickSortPopWindow.setBgView(binding.grayLayout);
        pickSortPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                sortTypeName = ((PickSelectEntity) key1).getProjectscale();
                sortType = ((PickSelectEntity) key1).getNo();
                updateChooseTextStatus(binding.tvSort, (Integer) key3, sortTypeName);
                requestBean.setSortMethod(sortType);
                initFilterData(bidTypeNo);

            }
        });
        pickSortPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.tvSort, Integer.valueOf(sortType) == 1 ? 0 : 1, sortTypeName);
            }
        });

        //数据来源筛选框
        pickSourcesPopWindow = new PickHallBidingPopWindow(getContext());
        pickSourcesPopWindow.setBgView(binding.grayLayout);
        pickSourcesPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                sourcesName = ((PickSelectEntity) key1).getProjectscale();
                sourcesType = ((PickSelectEntity) key1).getNo();
                if ("全部".equals(sourcesName)) {
                    sourcesName = "来源";
                }
                updateChooseTextStatus(binding.tvSources, (Integer) key3, sourcesName);
                initFilterData(bidTypeNo);

            }
        });
        pickSourcesPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.tvSources, Integer.valueOf(sourcesType) == 0 ? 0 : 1, sourcesName);
            }
        });

        //招标方式
        pickBidMethodPopWindow = new PickHallBidingPopWindow(getContext());
        pickBidMethodPopWindow.setBgView(binding.grayLayout);
        pickBidMethodPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
//                updateChooseStatus(PickCateType.TYPE_BIDDING, (Integer) key3);
//                initFilterData(bidTypeNo);
                bidMethodName = ((PickSelectEntity) key1).getProjectscale();
                bidMethodType = ((PickSelectEntity) key1).getNo();
                if ("全部".equals(bidMethodName)) {
                    bidMethodName = "招标方式";
                }
                updateChooseTextStatus(binding.tvBidMethod, (Integer) key3, bidMethodName);
                initFilterData(bidTypeNo);
            }
        });
        pickBidMethodPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
//                int value = getItemPos(PickCateType.TYPE_BIDDING);
//                updateChooseTextStatus(binding.tvBidMethod, value > 0 ? 1 : 0, bidMethodName);
                updateChooseTextStatus(binding.tvBidMethod, Integer.valueOf(bidMethodType) == 0 ? 0 : 1, bidMethodName);
            }
        });

        // 发布时间
        pushTimeWindow = new PickHallBidingPopWindow(getContext());
        pushTimeWindow.setBgView(binding.grayLayout);
        pushTimeWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                pushTimeName = ((PickSelectEntity) key1).getProjectscale();
                pushTimeNo = ((PickSelectEntity) key1).getNo();
                if ("不限".equals(pushTimeName)) {
                    pushTimeName = "发布时间";
                }
                updateChooseTextStatus(binding.tvStartTime, (Integer) key3, pushTimeName);
                requestBean.setPlanUseTime(pushTimeNo);
                initFilterData(bidTypeNo);
            }
        });
        pushTimeWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.tvStartTime, Integer.valueOf(pushTimeNo) == 0 ? 0 : 1, pushTimeName);
            }
        });

        //招标形式
        pickBidTypePopWindow = new PickHallBidingPopWindow(getContext());
        pickBidTypePopWindow.setBgView(binding.grayLayout);
        pickBidTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                bidTypeName = ((PickSelectEntity) key1).getProjectscale();
                bidTypeNo = ((PickSelectEntity) key1).getNo();
                if ("全部".equals(bidTypeName)) {
                    bidTypeName = "招标形式";
                }
                DemandData();
                updateChooseTextStatus(binding.tvBidType, (Integer) key3, bidTypeName);
                updateViewShow();
            }
        });
        pickBidTypePopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                updateChooseTextStatus(binding.tvBidType, Integer.valueOf(bidTypeNo) == 0 ? 0 : 1, bidTypeName);
            }
        });

        // 招标状态
        statusPopWindow = new PickHallBidingPopWindow(getContext());
        statusPopWindow.setBgView(binding.grayLayout);
        statusPopWindow.setOnResultClick((type, key2, key3) -> {
            updateChooseStatus(PickCateType.TYPE_BIDDING_STATUS, (Integer) key3);
            initFilterData(bidTypeNo);
        });
        statusPopWindow.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                int value = getItemPos(PickCateType.TYPE_BIDDING_STATUS);
                updateChooseTextStatus(binding.tvStatus, value > 0 ? 1 : 0, statusName);
            }
        });

        initAddressPopup();
        initCateGoryPopup();
    }

    public void DemandData() {
        if (bidTypeNo.equals("4") && commonlist != null && commonlist.size() > 0) {
            //有选二级默认设置二级type
            if (typeDatas.get(tabPosition).getProjectscale().equals("商品材料")) {
                requestBean.setCategoryId(commonlist.get(tabTypePosition).getId() + "");
            } else {
                requestBean.setCategoryId(commonlist.get(tabTypePosition).getNo() + "");
            }
        }
    }

    private void initCateGoryPopup() {
        popCategory = new ZCThreeLevelFilterWindow(getContext());
        popCategory.setBgView(binding.grayLayout);
        popCategory.setOnResultPositionClick(new ZCThreeLevelFilterWindow.OnResultPositionClick(){

            @Override
            public void resultPosition(Object key1, Object key2, Object key3, int pos1, int pos2, int pos3) {
                clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
                mFilters.put(PickCateType.TYPE_CATEGORY_0, tabTypePosition);

                if (pos1 > 0) {
                    List<PickSelectEntity> list2 = new ArrayList<>();
                    List<CommonPopEntity> cate1 = commonlist.get(tabTypePosition).getChildren();
                    if (cate1 != null && cate1.size() > 0) {
                        for (CommonPopEntity commonPopEntity : cate1) {
                            list2.add(new PickSelectEntity(commonPopEntity.getNo(), commonPopEntity.getTypeName()));
                        }
                    }
                    refreshDataInfo(PickCateType.TYPE_CATEGORY_1, "", list2);
                    updateChooseStatus(PickCateType.TYPE_CATEGORY_1, pos1);
                    if (pos2 > 0) {
                        List<PickSelectEntity> list3 = new ArrayList<>();
                        List<CommonPopEntity> cate2 = commonlist.get(tabTypePosition).getChildren().get(pos1).getChildren();
                        if (cate2 != null && cate2.size() > 0) {
                            for (CommonPopEntity commonPopEntity : cate2) {
                                list3.add(new PickSelectEntity(commonPopEntity.getNo(), commonPopEntity.getTypeName()));
                            }
                        }
                        refreshDataInfo(PickCateType.TYPE_CATEGORY_2, "", list3);
                        updateChooseStatus(PickCateType.TYPE_CATEGORY_2, pos2);
                    } else {
                        //重置
                        updateChooseStatus(PickCateType.TYPE_CATEGORY_0, 0);
                    }
                }

                CommonPopEntity pickResultProject1 = (CommonPopEntity) key1;
                CommonPopEntity pickResultProject2 = (CommonPopEntity) key2;
                CommonPopEntity pickResultProject3 = (CommonPopEntity) key3;
                if (!"全部".equals(pickResultProject1.getTypeName())) {
                    goodsTypeName = pickResultProject1.getTypeName();
                    goodsType = pickResultProject1.getNo();
                } else {
                    goodsTypeName = "";
                    goodsType = "";
                    popCategory.setData(childrenCommonlist, 0);
                }
                if (pickResultProject2 != null && !TextUtils.isEmpty(pickResultProject2.getTypeName())) {
                    if (!"全部".equals(pickResultProject2.getTypeName())) {
                        goodsTypeName = pickResultProject2.getTypeName();
                        goodsType = pickResultProject2.getNo();
                    } else {
                        goodsTypeName = "";
                        goodsType = "";
                    }
                }
                if (pickResultProject3 != null && !TextUtils.isEmpty(pickResultProject3.getTypeName())) {
                    if (!"全部".equals(pickResultProject3.getTypeName())) {
                        goodsTypeName = pickResultProject3.getTypeName();
                        goodsType = pickResultProject3.getNo();
                    } else {
                        goodsTypeName = "";
                        goodsType = "";
                    }
                }
                requestBean.setCategoryId(goodsType);
                updateChooseTextStatus(binding.tvCategory, TextUtils.isEmpty(goodsType) ? 0 : 1, goodsTypeName);
                //刷新数据
                initFilterData(bidTypeNo);

            }

            @Override
            public void reset() {
                goodsType = tabTypeList.get(tabTypePosition).getNo();
                goodsTypeName = "类别";
                popCategory.setData(childrenCommonlist, 0);
                updateChooseTextStatus(binding.tvCategory, 0, goodsTypeName);
                mFilters.put(PickCateType.TYPE_CATEGORY_1, 0);
                mFilters.put(PickCateType.TYPE_CATEGORY_2, 0);
                clearOtherDataInfo(PickCateType.TYPE_CATEGORY_1);
                //重置时，取二级类目no
                DemandData();
                //刷新数据
                initFilterData(bidTypeNo);
            }
        });

        popCategory.setOnDismissListener(() -> {
            if ("".equals(goodsTypeName) || "类别".equals(goodsTypeName)) {
                updateChooseTextStatus(binding.tvCategory, 0, goodsTypeName);
            } else {
                updateChooseTextStatus(binding.tvCategory, 1, goodsTypeName);
            }
        });
    }

    private List<PickSelectEntity> commonPopEntityToPickSelectEntity(List<CommonPopEntity> commonPopEntityList) {
        List<PickSelectEntity> list = new ArrayList<>();
        if (commonPopEntityList != null && commonPopEntityList.size() > 0) {
            for (CommonPopEntity commonPopEntity : commonPopEntityList) {
                list.add(new PickSelectEntity(commonPopEntity.getNo(), commonPopEntity.getTypeName()));
            }
        }
        return list;
    }

    private void initAddressPopup() {
        mAddressPop = new ThreeLevelAreaWindow(getContext());
        mAddressPop.setMemberHeight();
        mAddressPop.setCitySingle();
        mAddressPop.setProvinceSingle();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnlyShowProAndCity(true);
        mAddressPop.setAllSingleSelect(true);
        mAddressPop.setThreeListIsGone(true);
        mAddressPop.setBgView(binding.grayLayout);
        mAddressPop.setOnResultClick(new ThreeLevelAreaWindow.OnResultClickProxy() {
            @Override
            protected void result(GoodsSourceLocationEntity entity) {
                clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
                if (!TextUtils.isEmpty(entity.getProvinceNo())) {
                    areaName = entity.getProvinceName();
                    areacode = entity.getProvinceNo();
                    cityNo = "";
                    if ("0".equals(areacode)) {
                        areaName = "地区";
                    }
                    updateChooseTextStatus(binding.tvArea, 1, areaName);
                    mFilters.put(PickCateType.TYPE_PROVINCE, mAddressPop.getProvincePosition());
                    if (!TextUtils.isEmpty(entity.getCityNo())) {
                        cityNo = entity.getCityNo();
                        areaName = entity.getCityName();
                        viewModel.getAddressCityData(scrData, entity.getProvinceNo(), PickCateType.TYPE_CITY);
                        updateChooseTextStatus(binding.tvArea, 1, areaName);
                        mFilters.put(PickCateType.TYPE_CITY, mAddressPop.getmCityPosition());
                    }
                } else {
                    mFilters.put(PickCateType.TYPE_PROVINCE, 0);  //重置
                    mFilters.put(PickCateType.TYPE_CITY, 0);
                    areaName = "地区";
                    areacode = "";
                    cityNo = "";
                    updateChooseTextStatus(binding.tvArea, 0, areaName);
                }
                requestBean.setProvince(areacode);
                requestBean.setCity(cityNo);
                initFilterData(bidTypeNo);
            }
        });
        mAddressPop.setOnDismissListener(() -> {
            int pos1 = getItemPos(PickCateType.TYPE_PROVINCE);
            updateChooseTextStatus(binding.tvArea, pos1 > 0 ? 1 : 0, areaName);
        });
    }

    public void setTenderGoodsCategoryAllList(List<CommonPopEntity> data) {
        commonlist = data;
        tabTypeList.clear();
        binding.tabLayoutType.removeAllTabs();
        binding.tabLayoutType.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.translucent)));
        for (int i = 0; i < commonlist.size(); i++) {
            PickSelectEntity pickSelectEntity = new PickSelectEntity(commonlist.get(i).getNo(), commonlist.get(i).getTypeName());
            tabTypeList.add(pickSelectEntity);
            binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(pickSelectEntity.getProjectscale()));
            binding.tabLayoutType.getTabAt(i).setCustomView(R.layout.tab_item_textview);

            TextView tv = binding.tabLayoutType.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item_textview);
            tv.setTextSize(14);
            tv.setTextColor(getResources().getColor(R.color.color_666666));
            if (pickSelectEntity.getProjectscale().length() > 4) {
                tv.setText(pickSelectEntity.getProjectscale().substring(0, 4) + "...");
            } else {
                if (i == 0) {
                    tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    if (isAdded()) {
                        tv.setTextColor(getResources().getColor(R.color.APPTabColor));
                    }
                }
                tv.setText(pickSelectEntity.getProjectscale());
            }
        }

        tabTypePosition = getArguments().getInt("tabTypePosition");
        binding.tabLayoutType.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.tabLayoutType.getTabAt(tabTypePosition).select(); //默认选中某项放在加载viewpager之后
            }
        }, 5);
        refreshDataInfo(PickCateType.TYPE_CATEGORY_0, "类别", tabTypeList);

    }

    private void getCategoryAllList() {
        viewModel.getTenderGoodsCategoryAllList(typeNo);
    }

    private void initFilterData(String no) {
        page = 1;
        requestBean.page = page;
        isLoadMore = false;
        switch (no) {
            case "0":
                if ("1".equals(fromWhere)) {     //招标大厅
                    viewModel.findTenderingHomeEsPage(page,typeNo, goodsType, areacode, cityNo, searchName ,sortType,sourcesType);
                } else if ("2".equals(fromWhere)) {  //招标公告
                    viewModel.findNoticeHomeEsPage(page,typeNo, goodsType, areacode, cityNo, searchName,sortType,sourcesType);
                }
                break;
            case "1":
                if ("1".equals(fromWhere)) {     //招标大厅
                    viewModel.getHallList(page,cityNo, typeNo, price, statusNo, searchName, tenderApplyNo, areacode, goodsType,  sortType, endTime, startTime,bidMethodType);
                } else if ("2".equals(fromWhere)) {  //招标公告
                    viewModel.getHallBidingList(page,cityNo, typeNo, price, startTime, searchName, tenderApplyNo, areacode, goodsType, sortType,bidMethodType);
                }
                break;
            case "2":
                viewModel.getBiddingList(page,cityNo, typeNo, statusNo, price, searchName, areacode, goodsType, sortType, "",fromWhere);
                break;
            case "3":
                viewModel.getPreBiddingList(page,searchName, typeNo, goodsType, "", tenderApplyNo, startTime, price, areacode, cityNo, sortType, "");
                break;
            case "4":
                requestBean.setAddrDetailLat(latitude + "");
                requestBean.setAddrDetailLng(longitude + "");
                if (!TextUtils.isEmpty(searchName)) {
                    requestBean.setKeyword(searchName);
                } else {
                    requestBean.setKeyword("");
                }
                viewModel.getMaterialHomeList(typeNo, requestBean);
                break;
        }
    }

    private void initViewData() {
        binding.setViewModel(viewModel);
        binding.topLayout.rightView.setText("发布");
        binding.topLayout.tvSearch.setText("搜索");
        binding.topLayout.etView.setHint("搜索招标名称、采购单位");
        binding.topLayout.etView.setTextColor(this.getResources().getColor(R.color.color_tab_one));
        binding.topLayout.rightView.setOnClickListener(this);
        binding.topLayout.tvSearch.setOnClickListener(this);
        binding.topLayout.etView.setOnClickListener(this);
        binding.topLayout.ivBack.setOnClickListener(this);
        binding.topLayout.tvSearchDelete.setVisibility(View.GONE);
        binding.topLayout.tvSearchDelete.setOnClickListener(this);

        if (!TextUtils.isEmpty(searchName)) {
            binding.topLayout.etView.setText(searchName);
            binding.topLayout.tvSearchDelete.setVisibility(View.VISIBLE);
            initFilterData(bidTypeNo);
        }

        binding.tvBidType.setOnClickListener(this);
        binding.tvSort.setOnClickListener(this);
        binding.tvApply.setOnClickListener(this);
        binding.tvStatus.setOnClickListener(this);
        binding.tvStartTime.setOnClickListener(this);
        binding.tvMoney.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);
        binding.tvCategory.setOnClickListener(this);
        binding.imgBackTop.setOnClickListener(this);
        binding.rlMapList.setOnClickListener(this);
        binding.tvSort.setOnClickListener(this);
        binding.surveyView.setOnClickListener(this);
        binding.tvSources.setOnClickListener(this);
        binding.tvBidMethod.setOnClickListener(this);
        binding.ivQuickEntry.setOnClickListener(this);

        binding.topLayout.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.topLayout.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (isSearching) return true;
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(binding.topLayout.etView.getText().toString())) {
                        MToast.showToast(getContext(), "请输入搜索内容");
                        return true;
                    }
                    if (searchName.equals(binding.topLayout.etView.getText().toString())) {
                        MToast.showToast(getContext(), "请输入不同的搜索内容");
                        return true;
                    }
                    isSearching = true;
                    searchName = binding.topLayout.etView.getText().toString();
                    if (!TextUtils.isEmpty(searchName)) {
                        initFilterData(bidTypeNo);
                    }
                }
                return false;
            }
        });
        binding.topLayout.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    binding.topLayout.tvSearchDelete.setVisibility(View.GONE);
                } else {
                    binding.topLayout.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });

        int screenHeight = SizeUtils.getScreenHeight(getActivity());
        binding.scrollview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                RecyclerView.LayoutManager layoutManager = binding.recyclerView.getLayoutManager();
                View view = layoutManager.findViewByPosition(0);
                if (view != null) {
                    int height = view.getMeasuredHeight();
                    int headHeight = screenHeight - binding.llContainer.getHeight();
                    int gonePosition = 10 - (headHeight / view.getHeight());
                    binding.imgBackTop.setVisibility((height * gonePosition) > scrollY ? View.GONE : View.VISIBLE);
                }
            }
        });

        binding.swipeRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isLoadMore = true;
                requestBean.page = page;
                switch (bidTypeNo) {
                    case "0":
                        if ("1".equals(fromWhere)) {     //招标大厅
                            viewModel.findTenderingHomeEsPage(page,typeNo, goodsType, areacode, cityNo, searchName ,sortType,sourcesType);
                        } else if ("2".equals(fromWhere)) {  //招标公告
                            viewModel.findNoticeHomeEsPage(page,typeNo, goodsType, areacode, cityNo, searchName,sortType,sourcesType);
                        }
                        break;
                    case "1":
                        if ("1".equals(fromWhere)) {     //招标大厅
                            viewModel.getHallList(page,cityNo, typeNo, price, statusNo, searchName, tenderApplyNo, areacode, goodsType,  sortType, endTime, startTime,bidMethodType);
                        } else if ("2".equals(fromWhere)) {  //招标公告
                            viewModel.getHallBidingList(page,cityNo, typeNo, price, startTime, searchName, tenderApplyNo, areacode, goodsType, sortType,bidMethodType);
                        }
                        break;
                    case "2":
                        viewModel.getBiddingList(page,cityNo, typeNo, statusNo, price, searchName, areacode, goodsType, sortType, "",fromWhere);
                        break;
                    case "3":
                        viewModel.getPreBiddingList(page,searchName, typeNo, goodsType, "", tenderApplyNo, startTime, price, areacode, cityNo, sortType, "");
                        break;
                    case "4":
                        requestBean.setAddrDetailLat(latitude + "");
                        requestBean.setAddrDetailLng(longitude + "");
                        if (!TextUtils.isEmpty(searchName)) {
                            requestBean.setKeyword(searchName);
                        } else {
                            requestBean.setKeyword("");
                        }
                        viewModel.getMaterialHomeList(typeNo, requestBean);
                        break;
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initFilterData(bidTypeNo);
                binding.swipeRefresh.finishRefresh(300);
            }
        });
        //解决 在实现 上拉加载更多 和 下拉刷新 时  遇到item不在顶端就执行了刷新的方法和数据item没有填满屏幕也执行了加载更多
        binding.swipeRefresh.setScrollBoundaryDecider(new ScrollBoundaryDecider() {
            @Override
            public boolean canRefresh(View content) {
                if (binding.scrollview == null) return false;
                if (binding.scrollview.computeVerticalScrollOffset() == 0)
                    return true;
                return false;
            }

            @Override
            public boolean canLoadMore(View content) {
                if (binding.scrollview == null) return false;
                //获取recyclerView的高度
                binding.scrollview.getHeight();
                //整个View控件的高度
                int scrollRange = binding.scrollview.computeVerticalScrollRange();
                //当前屏幕之前滑过的距离
                int scrollOffset = binding.scrollview.computeVerticalScrollOffset();
                //当前屏幕显示的区域高度
                int scrollExtent = binding.scrollview.computeVerticalScrollExtent();
                int height = binding.scrollview.getHeight();
                if (height > scrollRange) {
                    return false;
                }
                if (isEnableLoadMore && scrollRange <= scrollOffset + scrollExtent) {
                    return true;
                }
                return false;
            }
        });

        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide2));
        binding.recyclerView.addItemDecoration(divider);

    }

    private void initAdapter() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        binding.recyclerView.setFocusable(false);

        if ("0".equals(bidTypeNo)) {  //全部

            if (pickHallAllNewAdapter == null) {
                pickHallAllNewAdapter = new PickHallAllNewAdapter(getContext(), null);
            }
            binding.recyclerView.setAdapter(pickHallAllNewAdapter);
            pickHallAllNewAdapter.onIChildClick(new PickHallAllNewAdapter.IChildClick() {
                @Override
                public void onClick(TenderAllListNewEntity tenderAllListNewEntity) {
                    if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                        if (tenderAllListNewEntity.getBusinessType() == 1) {
                            if (tenderAllListNewEntity.getState() == 8) {
                                PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                            } else {
                                TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                            }
                        } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                            viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                        }
                    } else if (tenderAllListNewEntity.getDataSources() == 2) {
                        viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                    }
                }
            });
            pickHallAllNewAdapter.setOnItemClick(new AdapterOnItemClick<TenderAllListNewEntity>() {
                @Override
                public void onItemClick(TenderAllListNewEntity tenderAllListNewEntity, int position) {
                    if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                        if (tenderAllListNewEntity.getBusinessType() == 1) {
                            if (tenderAllListNewEntity.getState() == 8) {
                                PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                            } else {
                                TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                            }
                        } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                            viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                        }
                    } else if (tenderAllListNewEntity.getDataSources() == 2) {
                        viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                    }
                }
            });

        } else if ("1".equals(bidTypeNo)) {   //0招标大厅 招标公告     1招标公告 招标报名

            if (pickHallAdapter == null) {
                pickHallAdapter = new PickHallAllNewAdapter(getContext(), null);
            }
            binding.recyclerView.setAdapter(pickHallAdapter);
            pickHallAdapter.onIChildClick(new PickHallAllNewAdapter.IChildClick() {
                @Override
                public void onClick(TenderAllListNewEntity tenderAllListNewEntity) {
                    if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                        if (tenderAllListNewEntity.getBusinessType() == 1) {
                            if (tenderAllListNewEntity.getState() == 8) {
                                PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                            } else {
                                TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                            }
                        } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                            viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                        }
                    } else if (tenderAllListNewEntity.getDataSources() == 2) {
                        viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                    }
                }
            });
            pickHallAdapter.setOnItemClick(new AdapterOnItemClick<TenderAllListNewEntity>() {
                @Override
                public void onItemClick(TenderAllListNewEntity tenderAllListNewEntity, int position) {
                    if (tenderAllListNewEntity.getDataSources() == 1) { //1.经营帮 2.政府网站
                        if (tenderAllListNewEntity.getBusinessType() == 1) {
                            if (tenderAllListNewEntity.getState() == 8) {
                                PickNoticeDetailActivity.start(getActivity(), tenderAllListNewEntity.getId() + "");
                            } else {
                                TenderDetailsActivity3.start(getContext(), tenderAllListNewEntity.getId() + "", 0);
                            }
                        } else if (tenderAllListNewEntity.getBusinessType() == 2) {
                            viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                        }
                    } else if (tenderAllListNewEntity.getDataSources() == 2) {
                        viewModel.getUrlSettingSj(tenderAllListNewEntity.getId());
                    }
                }
            });

        } else if ("2".equals(bidTypeNo)) {   //直接竞价

            if (pickBidingAdapter == null) {
                pickBidingAdapter = new PickHallAllNewAdapter(getContext(), null);
            }
            binding.recyclerView.setAdapter(pickBidingAdapter);
            pickBidingAdapter.setOnItemClick(new AdapterOnItemClick<TenderAllListNewEntity>() {
                @Override
                public void onItemClick(TenderAllListNewEntity tenderAllListNewEntity, int position) {
                    viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                }
            });
            pickBidingAdapter.onIChildClick(new PickHallAllNewAdapter.IChildClick() {
                @Override
                public void onClick(TenderAllListNewEntity tenderAllListNewEntity) {
                    viewModel.getUrlSetting(tenderAllListNewEntity.getId());
                }
            });

        } else if ("3".equals(bidTypeNo)) {   //招标预告

            if (pickNoticeListAdapter == null) {
                pickNoticeListAdapter = new PickNoticeListAdapter(getContext(), null);
            }
            binding.recyclerView.setAdapter(pickNoticeListAdapter);
            pickNoticeListAdapter.onIChildClick(new PickNoticeListAdapter.IChildClick() {
                @Override
                public void onClick(PreNoticeItemEntity preNoticeItemEntity) {
                    if (preNoticeItemEntity.getTenderState() == 8) {
                        PickNoticeDetailActivity.start(getContext(), preNoticeItemEntity.getTenderNo() + "");
                    } else {
                        TenderDetailsActivity3.start(getContext(), preNoticeItemEntity.getId() + "", 0);
                    }
                }
            });
            pickNoticeListAdapter.setOnItemClick(new AdapterOnItemClick<PreNoticeItemEntity>() {
                @Override
                public void onItemClick(PreNoticeItemEntity preNoticeItemEntity, int position) {
                    if (preNoticeItemEntity.getTenderState() == 8) {
                        PickNoticeDetailActivity.start(getContext(), preNoticeItemEntity.getTenderNo() + "");
                    } else {
                        TenderDetailsActivity3.start(getContext(), preNoticeItemEntity.getId() + "", 0);
                    }
                }
            });
        } else if ("4".equals(bidTypeNo)) {
            if (demandlistadapter == null) {
                demandlistadapter = new DemandListAdapter(getContext(), null);
            }

            binding.recyclerView.setAdapter(demandlistadapter);
            demandlistadapter.setOnItemClick(new AdapterOnItemClick<DemandForecastListEntity>() {
                @Override
                public void onItemClick(DemandForecastListEntity demandForecastListEntity, int position) {
                    viewModel.getWebH5(demandForecastListEntity.getId(), demandForecastListEntity.getModuleType());
                }
            });
            demandlistadapter.setOnItemStateOperate(new DemandListAdapter.OnItemStateOperate() {
                @Override
                public void OnItem(DemandForecastListEntity entity) {
                    viewModel.getWebH5(entity.getId(), entity.getModuleType());
                }
            });
        }

        if(tabPosition == 0){
            initFilterData(bidTypeNo);
        }

    }

    @Override
    protected void onLazyLoad() {
        initData();
        initAdapter();
    }

    @Override
    protected void initView() {
        super.initView();
        longitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
        latitude = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
        if (requestBean == null) {
            requestBean = new DemandForecastListBean();
        }
        init();
        initViewData();
    }

    private void initData() {
        //类型
        viewModel.getFilterTypeData();
        //预算金额
        viewModel.getPriceData();
        //数据来源筛选框
        viewModel.getDataSources();
        //招标形式接口
        viewModel.getBiddingMethod();
        //综合排序
        viewModel.getSortTypeData(bidTypeName);
        //时间
        viewModel.getPickTimes();
        //发布时间
        viewModel.getTime();
        viewModel.getProvinceData(PickCateType.TYPE_PROVINCE);
        //招标状态
        viewModel.getFilterStatusData();
        //招标类型
        viewModel.getTenderNewTypeList();
        //报名开始时间
        String[] stringTime = new String[]{"全部", "今天&1", "3天内&3", "5天内&5", "10天内&10", "30天内&30", "30天以上&31"};
        setStartTimeData(stringTime);
    }

    /****
     * 初始化置顶监听
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                getActivity().finish();
                break;
            case R.id.right_view:
                PickUtil.showBottomZhaoCaiDialog(getContext());
                break;
            case R.id.tv_money:
                showMoneyPopWindow();
                break;
            case R.id.tv_area:
                showProvincePopWindow();
                break;
            case R.id.tv_search:
                if (!TextUtils.isEmpty(binding.topLayout.etView.getText().toString())) {
                    searchName = binding.topLayout.etView.getText().toString();
                    initFilterData(bidTypeNo);
                }
                break;
            case R.id.tv_search_delete:
                binding.topLayout.etView.setText("");
                searchName = "";
                initFilterData(bidTypeNo);
                break;
            case R.id.tv_category:
                if (TextUtils.isEmpty(goodsTypeName)) {
                    goodsTypeName = "类别";
                }
                updateChooseTextStatus(binding.tvCategory, TextUtils.isEmpty(goodsType) ? -1 : -2, goodsTypeName);
//                popCategory.setData(childrenCommonlist, getItemPos(PickCateType.TYPE_CATEGORY_1), getItemPos(PickCateType.TYPE_CATEGORY_2));
                new Handler().postDelayed(() -> popCategory.showPopwindow0(binding.tvCategory), 200);
                break;
            case R.id.img_back_top:
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollTo(0, 0);
                break;
            case R.id.rl_map_list:
                if (!TextUtils.isEmpty(typeNo)) {
                    mFilters.put(PickCateType.TYPE_CATEGORY_0, tabTypePosition);
                    mLeftDialog2.initCategory(Integer.parseInt(typeNo), false, true);
                    mLeftDialog2.initData(mData, mFilters);
                    mLeftDialog2.showDialog();
                }
                break;
            case R.id.tv_sort:
                showSortPopWindow();
                break;
            case R.id.tv_sources:
                if (TextUtils.isEmpty(sourcesName)) {
                    sourcesName = "来源";
                    pickSourcesPopWindow.setDefItem();
                }
                updateChooseTextStatus(binding.tvSources, Integer.valueOf(sourcesType) == 1 ? -1 : -2, sourcesName);
                new Handler().postDelayed(() -> pickSourcesPopWindow.showPopwindow(binding.tvSources, 0, 0), 300);
                pickSourcesPopWindow.setData(dataSources, false);
                break;
            case R.id.tv_bid_method:
                showBidMethodPopWindow();
                break;
            case R.id.survey_view:
                if (tabTypeList == null || tabTypeList.size() == 0) return;
                binding.surveyView.setImageResource(R.drawable.btn_shop_up);
                pickTabLayoutPopWindow.showPopwindow(binding.surveyView);
                pickTabLayoutPopWindow.setData(tabTypeList, binding.tabLayoutType.getSelectedTabPosition());
                break;
            case R.id.tv_apply: //资格审查方式
                List<PickSelectEntity> applyDatas = getItemData(PickCateType.TYPE_SIGN);
                if (null != applyDatas && applyDatas.size() > 0) {
                    if (TextUtils.isEmpty(tenderApplyName)) {
                        tenderApplyName = "资格审查方式";
                    }
                    updateChooseTextStatus(binding.tvApply, TextUtils.isEmpty(tenderApplyNo) ? -1 : -2, tenderApplyName);
                    applyPopWindow.showPopwindow0(binding.tvApply);
                    int value = getItemPos(PickCateType.TYPE_SIGN);
                    applyPopWindow.updateData(value, getItemData(PickCateType.TYPE_SIGN));
                }
                break;
            case R.id.tv_status:   //招标状态
                List<PickSelectEntity> statusDatas = getItemData(PickCateType.TYPE_BIDDING_STATUS);
                if (statusDatas != null && statusDatas.size() > 0) {
                    if (TextUtils.isEmpty(statusName)) {
                        statusName = "招标状态";
                    }
                    updateChooseTextStatus(binding.tvStatus, TextUtils.isEmpty(statusNo) ? -1 : -2, statusName);
                    statusPopWindow.showPopwindow0(binding.tvStatus);
                    int value = getItemPos(PickCateType.TYPE_BIDDING_STATUS);
                    statusPopWindow.updateData(value, getItemData(PickCateType.TYPE_BIDDING_STATUS));
                }
                break;
            case R.id.tv_bid_type:
                if (TextUtils.isEmpty(bidTypeName) || "招标形式".equals(bidTypeName)) {
                    bidTypeName = "招标形式";
                    pickBidTypePopWindow.setDefItem();
                }
                updateChooseTextStatus(binding.tvBidType, "0".equals(bidTypeNo) ? -1 : -2, bidTypeName);
                pickBidTypePopWindow.showPopwindow0(binding.tvBidType);
                pickBidTypePopWindow.setData(bidTypeData, false);
                break;
            case R.id.tv_start_time:
                if (TextUtils.isEmpty(pushTimeName)) {
                    pushTimeName = "发布时间";
                    pushTimeWindow.setDefItem();
                }
                updateChooseTextStatus(binding.tvStartTime, "0".equals(pushTimeNo) ? -1 : -2, pushTimeName);
                pushTimeWindow.showPopwindow0(binding.tvStartTime);
                pushTimeWindow.setData(pushTimeData, false);
                break;
            case R.id.iv_quick_entry:
                EventBus.getDefault().postSticky(new QuickEntryEvent(binding.ivQuickEntry, new BackResultContentListener() {
                    @Override
                    public boolean backResult(QuickEntryEntity entity) {
                        switch (entity.getId()) {
                            case "2-1": //招标大厅
                                fromWhere = "1";
                                viewModel.tenderWayByType(typeNo, fromWhere);
                                initFilterData(bidTypeNo);
                                return true;
                            case "2-2": //招标公告
                                fromWhere = "2";
                                viewModel.tenderWayByType(typeNo, fromWhere);
                                initFilterData(bidTypeNo);
                                return true;
                            default:
                                getActivity().finish();
                                return false;
                        }
                    }
                }));
                break;
        }
    }

    public void getAllData(List<TenderAllListNewEntity> list) {
        isSearching = false;
        if (list != null) {
            isEnableLoadMore = list.size() >= 10;
        }
        binding.swipeRefresh.finishLoadMore(true);
        if (isLoadMore) {
            if (list.size() > 0) {
                pickHallAllNewAdapter.addDatas(list);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                pickHallAllNewAdapter.setDatas(list);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollToPosition(0);
                binding.scrollview.scrollTo(0, 0);
            } else {
                pickHallAllNewAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showHallList(List<TenderAllListNewEntity> list) {
        isSearching = false;
        if (list != null) {
            isEnableLoadMore = list.size() >= 10;
        }
        binding.swipeRefresh.finishLoadMore(true);
        if (isLoadMore) {
            if (list.size() > 0) {
                pickHallAdapter.addDatas(list);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                pickHallAdapter.setDatas(list);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollToPosition(0);
                binding.scrollview.scrollTo(0, 0);
            } else {
                pickHallAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 列表返回数据
     */
    public void setDemandList(List<DemandForecastListEntity> list, boolean isLastPage) {
        isSearching = false;
        if (list != null) {
            isEnableLoadMore = list.size() >= 10;
        }
        binding.swipeRefresh.finishLoadMore(true);
        if (isLoadMore) {
            if (list.size() > 0) {
                demandlistadapter.addDatas(list);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                demandlistadapter.setDatas(list);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollToPosition(0);
                binding.scrollview.scrollTo(0, 0);
            } else {
                demandlistadapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showBiddingList(List<TenderAllListNewEntity> list) {
        isSearching = false;
        if (list != null) {
            isEnableLoadMore = list.size() >= 10;
        }
        binding.swipeRefresh.finishLoadMore(true);
        if (isLoadMore) {
            if (list.size() > 0) {
                pickBidingAdapter.addDatas(list);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                pickBidingAdapter.setDatas(list);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollToPosition(0);
                binding.scrollview.scrollTo(0, 0);
            } else {
                pickBidingAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void showPreBiddingList(List<PreNoticeItemEntity> list) {
        isSearching = false;
        if (list != null) {
            isEnableLoadMore = list.size() >= 10;
        }
        binding.swipeRefresh.finishLoadMore(true);
        if (isLoadMore) {
            if (list.size() > 0) {
                pickNoticeListAdapter.addDatas(list);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                pickNoticeListAdapter.setDatas(list);
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.scrollToPosition(0);
                binding.scrollview.scrollTo(0, 0);
            } else {
                pickNoticeListAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setTimeDatas(List<PickSelectEntity> datas) {
        endTimeData.clear();
        endTimeData.addAll(datas);
        refreshDataInfo(PickCateType.TYPE_END_TIME, "报名截止时间", endTimeData);
        refreshDataInfo(PickCateType.TYPE_SIGN_TIME, "报名结束时间", endTimeData);
        refreshDataInfo(PickCateType.PUBLISH_TIME, "发布时间", endTimeData);
    }

    public void setTime(List<PickSelectEntity> datas) {
        pushTimeData.clear();
        pushTimeData.addAll(datas);
    }

    public void setBidTypeData(List<PickSelectEntity> datas) {
        bidTypeData.clear();
        bidTypeData.addAll(datas);
    }

    public void setStatusDatas(List<PickSelectEntity> datas) {
        List<PickSelectEntity> list = new ArrayList<>();
        for (PickSelectEntity entity : datas) {
            if (!"再次招标".equals(entity.getProjectscale())) {
                list.add(entity);
            }
        }
        statusData.clear();
        statusData.addAll(datas);
        refreshDataInfo(PickCateType.TYPE_BIDDING_STATUS, "招标状态", statusData);
    }

    public void setTenderTypeDatas(List<PickSelectEntity> datas) {
        signData.clear();
        signData.addAll(datas);
        refreshDataInfo(PickCateType.TYPE_SIGN, "资格审查方式", signData);
    }

    public void setPriceDatas(List<PickSelectEntity> datas) {
        priceData.clear();
        priceData.addAll(datas);
        refreshDataInfo(PickCateType.TYPE_MONEY, "预算金额", priceData);
    }

    /**
     * 报名开始时间
     */
    private void setStartTimeData(String[] strings) {
        for (String s : strings) {
            PickSelectEntity pickSelectEntity = new PickSelectEntity();
            if (s.contains("&")) {
                String[] splitText = s.split("&");
                pickSelectEntity.setNo(splitText[1]);
                pickSelectEntity.setProjectscale(splitText[0]);
            } else {
                pickSelectEntity.setNo("");
                pickSelectEntity.setProjectscale(s);
            }
            startTimeData.add(pickSelectEntity);
        }
    }

    public void setTypeDatas(List<PickSelectEntity> datas) {
        if (datas == null) return;
        typeDatas.clear();
        typeDatas.addAll(datas);
        for (int i = 0; i < typeDatas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab(), false);
            binding.tabLayout.getTabAt(i).setCustomView(R.layout.tab_item_textview);

            View view = binding.tabLayout.getTabAt(i).getCustomView();
            TextView tv = view.findViewById(R.id.tv_tab_item_textview);
            tv.setText(typeDatas.get(i).getProjectscale());
            tv.setTextSize(14);
            tv.setTextColor(getResources().getColor(R.color.color_333333));
        }

        if ("2".equals(getArguments().getString(FROM_WHERE))) {
            binding.tabLayout.getTabAt(0).select(); //默认选中某项放在加载viewpager之后
        } else {
            tabPosition = getArguments().getInt("tabPosition");
            if (tabPosition == -1) {
                tabPosition = 0;
            }
            binding.tabLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    binding.tabLayout.getTabAt(tabPosition).select(); //默认选中某项放在加载viewpager之后
                }
            }, 5);
        }

    }

    public void setCityDatas(List<PickSelectEntity> datas) {
        mLeftDialog2.addEntity(PickCateType.TYPE_CITY, datas);
        mLeftDialog2.showDialog();
    }

    public void setChooseItemData(int type, List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) return;
        switch (type) {
            case PickCateType.TYPE_PROVINCE:
                refreshDataInfo(PickCateType.TYPE_PROVINCE, "省份", data);
                break;
            case PickCateType.TYPE_CITY:
                refreshDataInfo(PickCateType.TYPE_CITY, "市区", data);
                break;
        }
    }

    public void setProvinceDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (data != null && data.size() > 0) {
            scrData = data;
            mAddressPop.setData(scrData, true);
        } else {
            viewModel.getProvinceData(PickCateType.TYPE_CITY);
        }
    }

    public void setSortDatas(List<PickSelectEntity> datas) {
        sortData.clear();
        sortData.addAll(datas);
    }

    public void setBidMethodDatas(List<PickSelectEntity> datas) {
        bidMethodData.clear();
        bidMethodData.addAll(datas);
        refreshDataInfo(PickCateType.TYPE_BIDDING, "招标方式", bidMethodData);
    }

    public void setDataSources(List<PickSelectEntity> datas) {
        dataSources.clear();
        dataSources.addAll(datas);
    }

    /**
     * 排序
     */
    private void showSortPopWindow() {
        if (TextUtils.isEmpty(sortTypeName)) {
            sortTypeName = "综合排序";
            pickSortPopWindow.setDefItem();
        }
        updateChooseTextStatus(binding.tvSort, Integer.valueOf(sortType) == 1 ? -1 : -2, sortTypeName);
        new Handler().postDelayed(() -> pickSortPopWindow.showPopwindow(binding.tvSort, 0, 0), 300);
        pickSortPopWindow.setData(sortData, false);
    }

    /**
     * 招标方式
     */
    private void showBidMethodPopWindow() {
//        if (TextUtils.isEmpty(bidMethodName)) {
//            bidMethodName = "招标方式";
//        }
//        updateChooseTextStatus(binding.tvBidMethod, TextUtils.isEmpty(bidMethodType) ? -1 : -2, bidMethodName);
//        new Handler().postDelayed(() -> pickBidMethodPopWindow.showPopwindow(binding.tvBidMethod, 0, 0), 300);
//        int value = getItemPos(PickCateType.TYPE_BIDDING);
//        pickBidMethodPopWindow.setData(getItemData(PickCateType.TYPE_BIDDING), value);

        if (TextUtils.isEmpty(bidMethodName)) {
            bidMethodName = "招标方式";
            pickBidMethodPopWindow.setDefItem();
        }
        updateChooseTextStatus(binding.tvBidMethod, TextUtils.isEmpty(bidMethodType) ? -1 : -2, bidMethodName);
        new Handler().postDelayed(() -> pickBidMethodPopWindow.showPopwindow(binding.tvBidMethod, 0, 0), 300);
        pickBidMethodPopWindow.setData(bidMethodData, false);
    }

    /**
     * 省份
     */
    private void showProvincePopWindow() {
        if (scrData == null) {
            viewModel.getProvinceData(PickCateType.TYPE_PROVINCE);
            return;
        }
        if (TextUtils.isEmpty(areaName)) {
            areaName = "地区";
        }
        updateChooseTextStatus(binding.tvArea, TextUtils.isEmpty(areacode) && TextUtils.isEmpty(cityNo) ? -1 : -2, areaName);
        mAddressPop.showPopwindow0(binding.tvArea);
    }

    /**
     * 预算金额
     */
    private void showMoneyPopWindow() {
        if (TextUtils.isEmpty(priceName)) {
            priceName = "预算金额";
        }
        updateChooseTextStatus(binding.tvMoney, TextUtils.isEmpty(price) ? -1 : -2, priceName);
        new Handler().postDelayed(() -> pickMoneyPopWindow.showPopwindow(binding.tvMoney, 0, 0), 300);
        int value = getItemPos(PickCateType.TYPE_MONEY);
        pickMoneyPopWindow.setData(getItemData(PickCateType.TYPE_MONEY), value);
    }

    public void getUrl(String url, String id) {
        if (TextUtils.isEmpty(url)) return;
        WebViewX5HostActivity.start(getContext(), url, id);
    }

    private PickChooseLeftDialog2 mLeftDialog2;
    // 数据集
    private SparseArray<PickSelectTextEntity> mData = new SparseArray<>();
    private SparseIntArray mFilters = new SparseIntArray();
    private SparseArray<String> nameArray = new SparseArray<>();

    private void initChooseDialog() {
        nameArray.put(PickCateType.TYPE_CATEGORY_0, "类别");
        nameArray.put(PickCateType.TYPE_CATEGORY_1, "类别1");
        nameArray.put(PickCateType.TYPE_CATEGORY_2, "类别2");
        nameArray.put(PickCateType.TYPE_BIDDING, "招标方式");
        nameArray.put(PickCateType.TYPE_SIGN, "资格审查方式");
        nameArray.put(PickCateType.TYPE_BIDDING_STATUS, "招标状态");
        nameArray.put(PickCateType.TYPE_START_TIME, "报名开始时间");
        nameArray.put(PickCateType.TYPE_END_TIME, "报名截止时间");
        nameArray.put(PickCateType.PUBLISH_TIME, "发布时间");
        nameArray.put(PickCateType.TYPE_MONEY, "预算金额");
        nameArray.put(PickCateType.TYPE_SIGN_TIME, "报名结束时间");
        nameArray.put(PickCateType.TYPE_PROVINCE, "省份");
        nameArray.put(PickCateType.TYPE_CITY, "市区");
        mLeftDialog2 = new PickChooseLeftDialog2(getContext(), nameArray);
        mLeftDialog2.setCallBack(this);
    }

    /**
     * 添加数据到中数据聚合
     */
    private void refreshDataInfo(int sort, String name, List<PickSelectEntity> children) {
        clearOtherDataInfo(sort);
        addEntity(sort, children, name);
    }

    /**
     * 清理关联项为空
     */
    private void clearOtherDataInfo(int sort) {
        switch (sort) {
            case PickCateType.TYPE_CATEGORY_0:
                addEntity(PickCateType.TYPE_CATEGORY_1, new ArrayList<>());
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_CATEGORY_1:
                addEntity(PickCateType.TYPE_CATEGORY_2, new ArrayList<>());
                break;
            case PickCateType.TYPE_PROVINCE:
                addEntity(PickCateType.TYPE_CITY, new ArrayList<>());
                break;
        }
    }

    public void addEntity(int index, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
        }
        mFilters.put(index, 0);
    }

    public void addEntity(int index, List<PickSelectEntity> list, String name) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
            mData.get(index).setTypeName(name);
        } else {
            mData.put(index, new PickSelectTextEntity(index, name, list));
        }
        mFilters.put(index, 0);
    }

    public void addEntity(int index, String name, List<PickSelectEntity> list) {
        if (mData.get(index) != null) {
            if (list == null || list.size() == 0) {
                mData.get(index).setPickSelectEntityList(new ArrayList<>());
            } else {
                mData.get(index).setPickSelectEntityList(list);
            }
            mData.get(index).setTypeName(name);
        } else {
            mData.put(index, new PickSelectTextEntity(index, nameArray.get(index), list));
            mFilters.put(index, 0);
        }
    }

    @Override
    public void loadCategory(PickSelectTextEntity entity, int itemPos) {
        List<PickSelectEntity> children = new ArrayList<>();
        if (itemPos > 0) {
            switch (entity.getSort()) {
                case PickCateType.TYPE_CATEGORY_0:
                    List<CommonPopEntity> cate1 = commonlist.get(itemPos).getChildren();
                    if (cate1 != null && cate1.size() > 0) {
                        for (CommonPopEntity commonPopEntity : cate1) {
                            children.add(new PickSelectEntity(commonPopEntity.getNo(), commonPopEntity.getTypeName()));
                        }
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_1, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_CATEGORY_1:
                    if (commonlist == null || commonlist.size() == 0) {
                        return;
                    }
                    int firPos = mLeftDialog2.getItemPos(0);
                    List<CommonPopEntity> cate2 = commonlist.get(firPos).getChildren().get(itemPos).getChildren();
                    if (cate2 != null && cate2.size() > 0) {
                        for (CommonPopEntity commonPopEntity : cate2) {
                            children.add(new PickSelectEntity(commonPopEntity.getNo(), commonPopEntity.getTypeName()));
                        }
                    }
                    mLeftDialog2.clearOtherDataInfo(entity.getSort());
                    mLeftDialog2.addEntity(PickCateType.TYPE_CATEGORY_2, children);
                    mLeftDialog2.showDialog();
                    break;
                case PickCateType.TYPE_PROVINCE:
                    String itemNo = entity.getItemNo(itemPos);
                    viewModel.getAddressCityDataDialog(scrData, itemNo);
                    break;
            }
        } else {
            if (entity.getSort() == PickCateType.TYPE_CATEGORY_0 || entity.getSort() == PickCateType.TYPE_CATEGORY_1 || entity.getSort() == PickCateType.TYPE_PROVINCE) {
                mLeftDialog2.clearOtherDataInfo(entity.getSort());
                mLeftDialog2.showDialog();
            }
        }
    }

    @Override
    public void loadData(SparseArray<PickSelectTextEntity> data, SparseIntArray map) {
        mData = PickCopyUtils.deepCopy(data);
        mFilters = map.clone();
        isLoadData = true;
        int index = 0;
        int index1 = 0;
        if (mFilters.size() > 0 && mFilters.get(0) > 0) {
            binding.tabLayoutType.getTabAt(mFilters.get(0)).select();
        }
        for (int i = 0; i < mData.size(); i++) {
            int key = mData.keyAt(i);
            PickSelectTextEntity entity = mData.get(key);
            if (i == 1) {
                index = mFilters.get(entity.getSort());
            }
            if (i == 2) {
                index1 = mFilters.get(entity.getSort());
            }
            updateChooseStatus(entity.getSort(), mFilters.get(entity.getSort()));
        }
        if (index > 0) {
            popCategory.setData(childrenCommonlist, index);
        }
        if (index1 > 0) {
            popCategory.setData(childrenCommonlist, index, index1);
        }
        isLoadData = false;
        initFilterData(bidTypeNo);
    }

    @Override
    public void reset() {
        mFilters.clear();
        clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
        clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
        loadData(mData, mFilters);
        mLeftDialog2.initData(mData, mFilters);
        mLeftDialog2.showDialog();
        if (binding.tabLayoutType.getTabAt(0) != null) {
            binding.tabLayoutType.getTabAt(0).select();
        }
    }

    @Override
    public void onCancel() {
        mLeftDialog2.initData(mData, mFilters);
    }

    /**
     * 获取当前缓存位置下标
     *
     * @param pos index
     */
    public int getItemPos(int pos) {
        return mFilters.get(pos);
    }

    public List<PickSelectEntity> getItemData(int index) {
        if (mData.get(index) != null) {
            return mData.get(index).getPickSelectEntityList();
        }
        return new ArrayList<>();
    }

    /**
     * 筛选赋值
     */
    private void updateChooseStatus(int key, int value) {
        mFilters.put(key, value);
        String no = mData.get(key).getItemNo(value);
        String name = mData.get(key).getItemName(value);
        switch (key) {
            case PickCateType.TYPE_CATEGORY_0:  //类别
            case PickCateType.TYPE_CATEGORY_1:
                if (TextUtils.isEmpty(no)) {
                    goodsTypeName = "类别";
                } else {
                    goodsType = no;
                    goodsTypeName = name;
                }
                updateChooseTextStatus(binding.tvCategory, value, goodsTypeName);
                break;
            case PickCateType.TYPE_CATEGORY_2:
                if (!TextUtils.isEmpty(no)) {
                    goodsType = no;
                    goodsTypeName = name;
                    if (value > 0) {
                        updateChooseTextStatus(binding.tvCategory, value, goodsTypeName);
                    }
                }
                break;
            case PickCateType.TYPE_MONEY: // 预算金额
                price = no;
                priceName = name;
                updateChooseTextStatus(binding.tvMoney, value, name);
                break;
            case PickCateType.TYPE_PROVINCE: // 省
                areacode = no;
                mAddressPop.setDefPosition(value, 0);
                if (value == 0) {
                    areaName = "地区";
                } else {
                    areaName = name;
                }
                updateChooseTextStatus(binding.tvArea, value, areaName);
                break;
            case PickCateType.TYPE_CITY: // 城市
                cityNo = no;
                mAddressPop.setDefPosition(mFilters.get(PickCateType.TYPE_PROVINCE), value);
                if (value > 0) {
                    areaName = name;
                    updateChooseTextStatus(binding.tvArea, value, areaName);
                }
                break;
            case PickCateType.TYPE_BIDDING_STATUS: // 招标状态
//                招标状态为“全部”、“招标报名”时，按 报名截止时间 筛选  （报名截止时间：放在 更多筛选 内）
//                招标状态为“招标预告”时，按 报名开始时间 筛选  （报名开始时间：放在 更多筛选 内）
//                招标状态为“正在招标”、“即将招标”、“竞标评标”、“招标定标”时：隐藏 报名开始时间/报名截止时间
                if ("招标状态".equals(name) || "招标报名".equals(name) || "全部".equals(name)) {
                    startTime = "";
                    addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", new ArrayList<>());
                    addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", endTimeData);
                } else if ("招标预告".equals(name)) {
                    endTime = "";
                    addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", startTimeData);
                    addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
                } else {
                    startTime = "";
                    endTime = "";
                    addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", new ArrayList<>());
                    addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
                }
                statusNo = no;
                statusName = name;
                updateChooseTextStatus(binding.tvStatus, value, name);
                break;
            case PickCateType.TYPE_SIGN: // 报名方式
                tenderApplyNo = no;
                tenderApplyName = name;
                updateChooseTextStatus(binding.tvApply, value, name);
                break;
            case PickCateType.TYPE_BIDDING: // 招标方式
                bidMethodType = no;
                bidMethodName = name;
                updateChooseTextStatus(binding.tvBidMethod, value, name);
                break;
        }
    }

    private void clearDialog() {
        mFilters.clear();
        clearOtherDataInfo(PickCateType.TYPE_PROVINCE);
        addEntity(PickCateType.PUBLISH_TIME, "发布时间", new ArrayList<>());
        addEntity(PickCateType.TYPE_MONEY, "预算金额", new ArrayList<>());
        addEntity(PickCateType.TYPE_BIDDING, "招标方式", new ArrayList<>());
        addEntity(PickCateType.TYPE_SIGN, "资格审查方式", new ArrayList<>());
        addEntity(PickCateType.TYPE_BIDDING_STATUS, "招标状态", new ArrayList<>());
        addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", new ArrayList<>());
        addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
        addEntity(PickCateType.TYPE_SIGN_TIME, "报名结束时间", new ArrayList<>());
    }

    private void clearPopWindow() {

        sortTypeName = "综合排序";
        sortType = "1";
        updateChooseTextStatus(binding.tvSort, 0, sortTypeName);
        pickSortPopWindow.setDefItem();

        sourcesName = "来源";
        sourcesType = "0";
        updateChooseTextStatus(binding.tvSources, 0, sourcesName);
        pickSourcesPopWindow.setDefItem();

        bidMethodName = "招标方式";
        bidMethodType = "0";
        updateChooseTextStatus(binding.tvBidMethod, 0, bidMethodName);
        pickBidMethodPopWindow.setDefItem();

        tenderApplyName = "资格审查方式";
        tenderApplyNo = "";
        updateChooseTextStatus(binding.tvApply, 0, tenderApplyName);
        applyPopWindow.setDefItem();

        statusName = "招标状态";
        statusNo = "";
        updateChooseTextStatus(binding.tvStatus, 0, statusName);
        statusPopWindow.setDefItem();

        pushTimeName = "发布时间";
        pushTimeNo = "0";
        updateChooseTextStatus(binding.tvStartTime, 0, pushTimeName);
        pushTimeWindow.setDefItem();

        priceName = "预算金额";
        price = "";
        updateChooseTextStatus(binding.tvMoney, 0, priceName);
        pickMoneyPopWindow.setDefItem();

        areacode = "";
        areaName = "";
        cityNo = "";
        updateChooseTextStatus(binding.tvArea, 0, areaName);
        mAddressPop.setDefPosition(0, 0);
    }

    private void clearViewShow() {
        binding.tvArea.setVisibility(View.VISIBLE);
        binding.tvSources.setVisibility(View.VISIBLE);

        binding.tvCategory.setVisibility(View.GONE);
        binding.tvApply.setVisibility(View.GONE);
        binding.tvStatus.setVisibility(View.GONE);
        binding.tvMoney.setVisibility(View.GONE);
        binding.tvStartTime.setVisibility(View.GONE);
        binding.tvBidMethod.setVisibility(View.GONE);

        binding.rlMapList.setVisibility(View.GONE);
        binding.topLayout.etView.setHint("搜索招标名称、采购单位");

        clearPopWindow();
        clearDialog();
        clearOtherDataInfo(PickCateType.TYPE_CATEGORY_0);
    }

    private void updateViewShow() {
        popCategory.setData(childrenCommonlist, 0);
        clearDialog();
        clearPopWindow();

        goodsTypeName = "";
        updateChooseTextStatus(binding.tvCategory, 0, goodsTypeName);

        initAdapter();
        viewModel.getSortTypeData(bidTypeName);
        switch (bidTypeNo) {
            case "0":
                binding.tvArea.setVisibility(View.VISIBLE);
                binding.tvSources.setVisibility(View.VISIBLE);
                binding.tvApply.setVisibility(View.GONE);
                binding.tvStatus.setVisibility(View.GONE);
                binding.tvMoney.setVisibility(View.GONE);
                binding.tvStartTime.setVisibility(View.GONE);
                binding.tvBidMethod.setVisibility(View.GONE);

                binding.rlMapList.setVisibility(View.GONE);
                binding.topLayout.etView.setHint("搜索招标名称、采购单位");

                break;
            case "1":
                if ("1".equals(fromWhere)) {
                    binding.tvStatus.setVisibility(View.VISIBLE);
                    binding.tvMoney.setVisibility(View.GONE);

//                    addEntity(PickCateType.TYPE_BIDDING, "招标方式", bidMethodData);
                    addEntity(PickCateType.TYPE_SIGN, "资格审查方式", signData);
                    addEntity(PickCateType.TYPE_BIDDING_STATUS, "招标状态", statusData);
                    addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", endTimeData);
                    addEntity(PickCateType.TYPE_MONEY, "预算金额", priceData);

                    addEntity(PickCateType.TYPE_SIGN_TIME, "报名结束时间", new ArrayList<>());

                } else if ("2".equals(fromWhere)) {
                    binding.tvMoney.setVisibility(View.VISIBLE);
                    binding.tvStatus.setVisibility(View.GONE);

//                    addEntity(PickCateType.TYPE_BIDDING, "招标方式", bidMethodData);
                    addEntity(PickCateType.TYPE_SIGN, "资格审查方式", signData);
                    addEntity(PickCateType.TYPE_MONEY, "预算金额", priceData);
                    addEntity(PickCateType.TYPE_SIGN_TIME, "报名结束时间", endTimeData);

                    addEntity(PickCateType.TYPE_BIDDING_STATUS, "招标状态", new ArrayList<>());
                    addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
                }

                binding.tvBidMethod.setVisibility(View.VISIBLE);
                binding.tvApply.setVisibility(View.VISIBLE);

                binding.tvSources.setVisibility(View.GONE);
                binding.tvArea.setVisibility(View.GONE);
                binding.tvStartTime.setVisibility(View.GONE);
                addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", new ArrayList<>());
                addEntity(PickCateType.PUBLISH_TIME, "发布时间", new ArrayList<>());


                binding.rlMapList.setVisibility(View.VISIBLE);
                binding.topLayout.etView.setHint("搜索招标名称、采购单位");
                break;
            case "2":
                binding.tvMoney.setVisibility(View.VISIBLE);
                binding.tvArea.setVisibility(View.VISIBLE);
                binding.tvApply.setVisibility(View.GONE);
                binding.tvStatus.setVisibility(View.GONE);
                binding.tvStartTime.setVisibility(View.GONE);
                binding.tvSources.setVisibility(View.GONE);
                binding.tvBidMethod.setVisibility(View.GONE);

                addEntity(PickCateType.PUBLISH_TIME, "发布时间", endTimeData);
                addEntity(PickCateType.TYPE_MONEY, "预算金额", priceData);

                addEntity(PickCateType.TYPE_BIDDING, "招标方式", new ArrayList<>());
                addEntity(PickCateType.TYPE_SIGN, "资格审查方式", new ArrayList<>());
                addEntity(PickCateType.TYPE_BIDDING_STATUS, "招标状态", new ArrayList<>());
                addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", new ArrayList<>());
                addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
                addEntity(PickCateType.TYPE_SIGN_TIME, "报名结束时间", new ArrayList<>());

                binding.rlMapList.setVisibility(View.VISIBLE);
                binding.topLayout.etView.setHint("搜索竞价名称、采购单位");
                break;
            case "3":
                binding.tvApply.setVisibility(View.VISIBLE);
                binding.tvArea.setVisibility(View.GONE);
                binding.tvStatus.setVisibility(View.GONE);
                binding.tvMoney.setVisibility(View.GONE);
                binding.tvStartTime.setVisibility(View.GONE);
                binding.tvSources.setVisibility(View.GONE);
                binding.tvBidMethod.setVisibility(View.GONE);

                addEntity(PickCateType.TYPE_SIGN, "资格审查方式", signData);
                addEntity(PickCateType.TYPE_START_TIME, "报名开始时间", startTimeData);
                addEntity(PickCateType.TYPE_MONEY, "预算金额", priceData);

                addEntity(PickCateType.TYPE_BIDDING, "招标方式", new ArrayList<>());
                addEntity(PickCateType.TYPE_BIDDING_STATUS, "招标状态", new ArrayList<>());
                addEntity(PickCateType.TYPE_END_TIME, "报名截止时间", new ArrayList<>());
                addEntity(PickCateType.PUBLISH_TIME, "发布时间", new ArrayList<>());
                addEntity(PickCateType.TYPE_SIGN_TIME, "报名结束时间", new ArrayList<>());

                binding.rlMapList.setVisibility(View.VISIBLE);
                binding.topLayout.etView.setHint("搜索招标名称、采购单位");
                break;
            case "4":
                binding.tvStartTime.setVisibility(View.VISIBLE);
                binding.tvArea.setVisibility(View.VISIBLE);
                binding.tvStatus.setVisibility(View.GONE);
                binding.tvMoney.setVisibility(View.GONE);
                binding.tvApply.setVisibility(View.GONE);
                binding.tvSources.setVisibility(View.GONE);
                binding.tvBidMethod.setVisibility(View.GONE);

                binding.rlMapList.setVisibility(View.GONE);
                binding.topLayout.etView.setHint("搜索需求预告名称");
                break;
        }
    }

    /**
     * 更新选项颜色
     */
    private void updateChooseTextStatus(PickPopWindowView pickPopWindowView, int value, String name) {
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > 4) {
                name = name.substring(0, 4) + "...";
            }
        }
        if (value == -1 || value == -2) {
            binding.popLine.setVisibility(View.VISIBLE);
            pickPopWindowView.setChooseData(R.drawable.member_pop_blue_bg2, R.mipmap.ic_up_arrow_n, R.color.color_333333, true, name);
        } else if (value == 0) {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333, false, name);
        } else {
            binding.popLine.setVisibility(View.GONE);
            pickPopWindowView.setChooseData(R.drawable.search_new_blue_bg_10, R.mipmap.icon_down_red, R.color.color_main, false, name);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ChooseAreaEvent event) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}